var categoryMap = {
    1:"创意设计",
    2:"基础部件",
    3:"整机套装",
    4:"应用配套"
};

var applyStatusMap = {
    1:"审核中",
    2:"审核失败",
    3:"审核通过",
    4:"初评中",
    5:"复评中",
    6:"撤销申请"
};
// 添加奖励
layui.use(['form', 'code', 'laydate','upload','layer','laytpl','element','laypage'], function() {
    var laydate = layui.laydate;
    form = layui.form;
    upload = layui.upload;
    layer = layui.layer;
    laytpl = layui.laytpl;
    element = layui.element;
    laypage = layui.laypage;



    layui.code();


    function lookApplyInfo(dom){
        var projectId = $(dom.target).attr("data-id");
        var projectList = $(".projectList").data("projectList");
        var applyProject =  null;
        $(".detailDiv").html(JSON.stringify(projectList));
        for(let i =0;i < projectList.length;i++){
            if(projectId == projectList[i].id){
                applyProject = projectList[i];
                break;
            }
        }
        $(".projectId").val(applyProject.id);
        //申请人信息
        $(".name").val(applyProject.applicant.name);
        $(".sex").val(applyProject.applicant.sex == 1?"男":"女");
        $(".photo").prop("src",applyProject.applicant.photo);
        $(".registeredAddress").val(applyProject.applicant.registeredProvince + applyProject.applicant.registeredCity + applyProject.applicant.registeredArea);
        $(".liveAddress").val(applyProject.applicant.liveProvince + applyProject.applicant.liveCity + applyProject.applicant.liveArea + " " + applyProject.applicant.address);
        //项目信息
        $(".projectName").val(applyProject.project.projectName);
        $(".projectContext").val(applyProject.project.projectContext);
        $(".serviceComment").val(applyProject.project.serviceComment);
        $(".marketUserComment").val(applyProject.project.marketUserComment);
        $(".businessPattern").val(applyProject.project.businessPattern);
        $(".runData").val(applyProject.project.runData);
        $(".coreResource").val(applyProject.project.coreResource);
        PDFObject.embed(applyProject.project.bpUrl,"#previewBp");

        $(".categoryName").val(applyProject.projectCategory.categoryName);
        $(".secondCategoryName").val(applyProject.projectCategory.secondCategoryName);
        $(".thirdCategoryName").val(applyProject.projectCategory.thirdCategoryName);
        $("#myModal").show();
    }



    //初始化列表
    setTimeout(function () {
        AJAX.post(cgiMain.academistApplyMe,
            {pageNum:1,pageSize:10},
            function (res) {

                if(res.code == '0000'){
                    $(".projectList").data("projectList",res.result.list);
                    var tpl = $("#projectItemTpl").html();
                    var resultPage = res.result;
                    for(var i = 0; i < resultPage.list.length;i++){
                        var item = resultPage.list[i];
                        item.statusName = applyStatusMap[item.status];
                        fillPriority(item);
                        laytpl(tpl).render(item,function (html) {
                            $(".projectList").append(html);
                            $(".projectList").on("click",'.lookApplyInfo',function (elem) {
                                lookApplyInfo(elem);

                            });
                            $(".projectList").on("click",'.givePriority',function (elem) {
                                givePriority(elem);

                            });
                        });
                    }
                    //执行一个laypage实例
                    laypage.render({
                        elem: 'pagination'
                        ,limit: 10
                        ,count: resultPage.totalCount
                        ,limits:[10]
                        ,theme:'#1E9FFF'
                        ,layout:['prev', 'page', 'next','limit','count']
                        ,jump: function (obj,first) {
                            if(first == true){
                                return;
                            }
                            $(".projectList").html("");
                            nextPage(obj.curr,10);

                        }
                    });
                    element.render(".projectList");
                }

            });

    },500);

    function fillPriority(item) {
        if(!item.priority){
            return ;
        }
        item.priorityStatus="已推优";
        item.priorityComment = item.priority.comment;
        item.priorityId=item.priority.priorityId;
    }

    function nextPage(pageNum,pageSize) {
        AJAX.post(cgiMain.academistApplyMe,
            {pageNum:pageNum,pageSize:pageSize},
            function (res) {

                if(res.code == '0000'){
                    $(".projectList").data("projectList",res.result.list);
                    var tpl = $("#projectItemTpl").html();
                    var resultPage = res.result;
                    for(var i = 0; i < resultPage.list.length;i++){
                        var item = resultPage.list[i];
                        item.statusName = applyStatusMap[item.status];
                        fillPriority(item);
                        laytpl(tpl).render(item,function (html) {
                            $(".projectList").append(html);
                            $(".projectList").on("click",'.lookApplyInfo',function (elem) {
                                lookApplyInfo(elem);

                            });
                            $(".projectList").on("click",'.givePriority',function (elem) {
                                givePriority(elem);

                            });
                        });
                    }

                    element.render(".projectList");
                }

            });
    }

    $("#toGivePriority").on("click",function () {
        let projectId = $(".projectId").val();
        $("#myModal").hide();
        $("#myModal input, #myModal textarea").val("");
        showPriority(projectId);
    });

    $(".deletePriority").click(function () {
        var params = {};

        // 集合所有参数
        $("#addNewPriorityForm .save_val").each(function() {
            var name = $(this).attr("name");
            var val = $(this).val();
            params[name] = val;
        });
        var url = cgiMain.academistApplyDeletePriority;
        AJAX.post(url,
            params,
            function (res) {
                if(res.code=='0000'){
                    layer.msg(res.msg);
                    window.location.reload();
                }
            });
    });

    $("#submitNewPriority").click(function () {
        // 判断必填项--提示--统校验
        var params = {};

        // 集合所有参数
        $("#addNewPriorityForm .save_val").each(function() {
            var name = $(this).attr("name");
            var val = $(this).val();
            params[name] = val;
        });

        var url = cgiMain.academistApplyGivePriority;
        if(params.id && params.id > 0){
            url = cgiMain.academistApplyEditPriority;
        }

        AJAX.post(url,
            params,
            function (res) {
                if(res.code=='0000'){
                    layer.msg('保存成功');
                    $("#myModal").hide();
                    window.location.reload();
                }
            });
        return false;
    });


    function givePriority(dom) {
        var projectId = $(dom.target).attr("data-id");
        showPriority(projectId);

    }
    function showPriority(projectId) {
        var projectList = $(".projectList").data("projectList");
        var applyProject =  null;
        // $(".detailDiv").html(JSON.stringify(projectList));
        for(let i =0;i < projectList.length;i++){
            if(projectId == projectList[i].id){
                applyProject = projectList[i];
                break;
            }
        }
        //申请人信息
        $(".applyId").val(applyProject.id);
        $(".academistId").val(applyProject.academistId);
        if(applyProject.priority){
            $(".id").val(applyProject.priority.id);
            $(".comment").val(applyProject.priority.comment);
            $(".deletePriority").show();
        }

        form.render();
        form.render('select');

        $("#priorityModal").show();
    }

    function getTypeName(secondCategory,thirdCategory) {
        var subjectTypeName = "";
        var params={type:5};
        $.ajax({
            url: cgiMain.getDict,
            dataType: 'json',
            data: params,
            xhrFields: {
                withCredentials: true
            },
            contentType: false,
            type: 'GET',
            cache: false,
            timeout: CFG.TIMEOUT,
            async:false,
            success: function(res) {
                if (res.code== '0000') {
                    subjectTypeName = res.result[secondCategory];
                }

            },
            error: function() {}});
        params={type:5,parentId:secondCategory};
        $.ajax({
            url: cgiMain.getDict,
            dataType: 'json',
            data: params,
            xhrFields: {
                withCredentials: true
            },
            contentType: false,
            type: 'GET',
            cache: false,
            timeout: CFG.TIMEOUT,
            async:false,
            success: function(res) {
                if (res.code== '0000') {
                    subjectTypeName =subjectTypeName + "/"  + res.result[thirdCategory];
                }

            },
            error: function() {}});

        return subjectTypeName;
    }



});



$(".modal-footer > .layui-btn-primary").on("click",function () {
    $("#myModal").hide();
    $("#myModal input, #myModal textarea").val("");

    $("#priorityModal").hide();
});



